﻿using FastExecutor.Base.Model;
using FastORM.Model;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace FastExecutor.Base.Design
{

    public class FastTemplateController<T> : BaseController, IFastBaseController<T> where T : BaseEntity
    {
        public FastTemplateDB<T> DB_Fast;
        public string ListWhereText = "1=1";
        public string OrderText = "OrderNum desc";

        [HttpPost, Route("FindAll")]
        public virtual ActionResult FindAll()
        {
            ReturnValue result = new ReturnValue();
            List<T> list = DB_Fast.FindAll();
            result.Add("data", list);
            return result;
        }

        [HttpPost, Route("FindList")]
        public virtual ActionResult FindList()
        {
            ReturnValue result = new ReturnValue();
            JObject Params = GetParams();
            int pageindex = Convert.ToInt32(Params["PageIndex"].ToString());
            int pagesize = Convert.ToInt32(Params["PageSize"].ToString());
            List<T> list = DB_Fast.FindPage("*", ListWhereText, OrderText, pageindex, pagesize);
            int total = DB_Fast.Count(ListWhereText);
            result.Add("total", total);
            result.Add("data", list);
            return result;
        }

        [HttpPost, Route("FindDetail")]
        public virtual ActionResult FindDetail()
        {
            ReturnValue result = new ReturnValue();
            JObject Params = GetParams();
            string keyguid = Convert.ToString(Params[DB_Fast.KeyName]);
            T entity = DB_Fast.FindOne(keyguid);
            result.Add("data", entity);
            return result;
        }

        [HttpPost, Route("OperateAdd")]
        public virtual ActionResult OperateAdd(T Entity)
        {
            ReturnValue result = new ReturnValue();
            DB_Fast.Insert(Entity);
            return result;
        }

        [HttpPost, Route("OperateUpdate")]
        public virtual ActionResult OperateUpdate()
        {
            ReturnValue result = new ReturnValue();
            JObject Params = GetParams();
            DB_Fast.Update(Params);
            return result;
        }

        [HttpPost, Route("OperateBatchUpdate")]
        public virtual ActionResult OperateBatchUpdate()
        {
            ReturnValue result = new ReturnValue();
            JObject Params = GetParams();
            JArray JsonArray = (JArray)JsonConvert.DeserializeObject(Convert.ToString(Params["UpdateList"]));
            DB_Fast.BatchUpdate(JsonArray);
            return result;
        }

        [HttpPost, Route("OperateDelete")]
        public virtual ActionResult OperateDelete()
        {
            ReturnValue result = new ReturnValue();
            JObject Params = GetParams();
            string keyguid = Convert.ToString(Params["KeyGuid"]);
            DB_Fast.Delete(keyguid);
            return result;
        }

        [HttpPost, Route("OperateBatchDelete")]
        public virtual ActionResult OperateBatchDelete()
        {
            ReturnValue result = new ReturnValue();
            JObject Params = GetParams();
            string keyguidlist = Convert.ToString(Params["KeyGuidList"]);
            DB_Fast.BatchDelete(keyguidlist.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToList());
            return result;
        }




    }
}
